﻿using Sidvall.IdentityManager.Data;
using Sidvall.IdentityManager.Net.SimpleServices.V1;
using Sidvall.Serialization;

namespace Sidvall.IdentityManager
{
    public static class ParameterManager
    {
        #region Public Members

        // Application Settings
        public const string DefaultServerItemParameterName = "DefaultServerItem";
        public const string DefaultUserConfigurationParameterName = "DefaultUserConfiguration";

        // Commands
        public const string CreateUserCommandId = "CreateUser";
        public const string ConfirmPasswordCommandId = "ConfirmPassword";
        public const string ResetPasswordCommandId = "ResetPassword";
        public const string VerifyPasswordCommandId = "VerifyPassword";
        public const string VerifyUserPasswordCommandId = "VerifyUserPassword";
        public const string VerifyTokenCommandId = "VerifyToken";
        public const string SendConfirmEmailCommandId = "SendConfirmEmail";
        public const string SendResetPasswordEmailCommandId = "SendResetPasswordEmail";
        public const string CreateSecurityStampCommandId = "CreateSecurityStamp";
        public const string FindUsersFromLoginCommandId = "FindUsersFromLogin";
        public const string GenerateTokenCommandId = "GenerateToken";

        // Command Parameters
        public const string UserIdParameterName = "UserId";
        public const string UserNameParameterName = "UserName";
        public const string EmailParameterName = "Email";
        public const string EmailConfirmedParameterName = "EmailConfirmed";
        public const string PhoneNumberParameterName = "PhoneNumber";
        public const string PhoneNumberConfirmedParameterName = "PhoneNumberConfirmed";
        public const string PasswordParameterName = "Password";
        public const string PasswordHashParameterName = "PasswordHash";
        public const string SecurityStampParameterName = "SecurityStamp";
        public const string NameParameterName = "Name";
        public const string PurposeParameterName = "Purpose";
        public const string TokenProviderParameterName = "TokenProvider";
        public const string TokenParameterName = "Token";
        public const string ResultParameterKey = "Result";
        public const string LoginProviderParameterKey = "LoginProvider";
        public const string ProviderKeyParameterKey = "ProviderKey";

        // Context keys
        public const string ServerContentKey = "ServerContentKey";

        #region DefaultServerItem

        private static System.Lazy<ServerItem> _DefaultServerItem = new System.Lazy<ServerItem>(GetDefaultServerItem);
        public static ServerItem DefaultServerItem
        {
            get
            {
                return _DefaultServerItem.Value;
            }
        }

        #endregion
        #region DefaultUserConfiguration

        private static System.Lazy<UserConfiguration> _DefaultUserConfiguration = new System.Lazy<UserConfiguration>(GetDefaultUserConfiguration);
        public static UserConfiguration DefaultUserConfiguration
        {
            get
            {
                return _DefaultUserConfiguration.Value;
            }
        }

        #endregion
        #region SmtpServer

        public static string SmtpServer
        {
            get
            {
                return Sidvall.IdentityManager.SystemContext.Current.UserConfiguration?.SmtpServer;
            }
        }

        #endregion
        #region ConfirmationEmailFromAddress

        public static string ConfirmationEmailFromAddress
        {
            get
            {
                return Sidvall.IdentityManager.SystemContext.Current.UserConfiguration?.ConfirmationEmailFromAddress;
            }
        }

        #endregion
        #region ConfirmationEmailSubject

        public static string ConfirmationEmailSubject
        {
            get
            {
                return Sidvall.IdentityManager.SystemContext.Current.UserConfiguration?.ConfirmationEmailSubject;
            }
        }

        #endregion
        #region ConfirmationEmailBody

        public static string ConfirmationEmailBody
        {
            get
            {
                return Sidvall.IdentityManager.SystemContext.Current.UserConfiguration?.ConfirmationEmailBody;
            }
        }

        #endregion
        #region ResetPasswordEmailFromAddress

        public static string ResetPasswordEmailFromAddress
        {
            get
            {
                return Sidvall.IdentityManager.SystemContext.Current.UserConfiguration?.ResetPasswordEmailFromAddress;
            }
        }

        #endregion
        #region ResetPasswordEmailSubject

        public static string ResetPasswordEmailSubject
        {
            get
            {
                return Sidvall.IdentityManager.SystemContext.Current.UserConfiguration?.ResetPasswordEmailSubject;
            }
        }

        #endregion
        #region ResetPasswordEmailBody

        public static string ResetPasswordEmailBody
        {
            get
            {
                return Sidvall.IdentityManager.SystemContext.Current.UserConfiguration?.ResetPasswordEmailBody;
            }
        }

        #endregion

        #region GetDefaultServerItem

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        private static ServerItem GetDefaultServerItem()
        {
            ServerItem item;
            string[] settings;
            string value;

            item = new ServerItem();
            value = Sidvall.SystemContext.Current.SettingsManager.GetSetting(DefaultServerItemParameterName).ToString(string.Empty);
            if (string.IsNullOrWhiteSpace(value))
                return item;
            settings = value.Split("|", System.StringSplitOptions.None);
            try
            {
                item = new ServerItem()
                {
                    Name = settings[0],
                    ConnectionTypeId = (Data.ConnectionType)settings[1].ToNullableInt32().Value,
                    ConnectionString = settings[2],
                    RemoteAddress = settings[3],
                    STSAddress = settings[4],
                };
            }
            catch
            {
                item = new ServerItem();
            }
            return item;
        }

        #endregion
        #region GetDefaultUserConfiguration

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        private static UserConfiguration GetDefaultUserConfiguration()
        {
            UserConfiguration item;
            string[] settings;
            string value;

            value = Sidvall.SystemContext.Current.SettingsManager.GetSetting(DefaultUserConfigurationParameterName).ToString(string.Empty);
            if (string.IsNullOrWhiteSpace(value))
            {
                return new UserConfiguration()
                {
                    UserConfigurationId = 1,
                    Name = "Default",
                    StartPageId = PageType.Dashboard,
                    ShowNewsAtStartup = true,
                };
            }
            settings = value.Split("|", System.StringSplitOptions.None);
            try
            {
                item = new UserConfiguration()
                {
                    UserConfigurationId = 1,
                    Name = "Default",
                    StartPageId = PageType.Dashboard,
                    ShowNewsAtStartup = true,
                    ResetPasswordEmailFromAddress = settings[0],
                    ResetPasswordEmailSubject = settings[1],
                    ResetPasswordEmailBody = settings[2],
                };
            }
            catch
            {
                item = new UserConfiguration()
                {
                    UserConfigurationId = 1,
                    Name = "Default",
                    StartPageId = PageType.Dashboard,
                    ShowNewsAtStartup = true,
                };
            }
            return item;
        }

        #endregion

        #endregion
    }
}
